package com.yuexunit.operationman.nbsy.module.Catheter.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.Catheter.model.impl.CatheterModel;
import com.yuexunit.operationman.nbsy.module.Catheter.presenter.ICatheterPresenter;
import com.yuexunit.operationman.nbsy.module.Catheter.presenter.impl.CatheterPresenter;
import com.yuexunit.operationman.nbsy.module.Catheter.ui.view.ICatheterView;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SignBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationSignModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationSignView;
import com.yuexunit.operationman.nbsy.widget.ActionSheetDialog;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/12/28 0028.
 */

public class Act_Catheter_Sign extends BaseKJActivity implements ISafeOperationSignView,ICatheterView {

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.img_sign_visit)
    ImageView imgSignVisit;
    @Bind(R.id.btn_sign_visit)
    TextView btnSignVisit;
    @Bind(R.id.tv_sign_visit_time)
    TextView tvSignVisitTime;
    @Bind(R.id.tv_sign_visit_name)
    TextView tvSignVisitName;

    LoginDialog loginDialog;
    LoadingDialog loadingDialog;

    ICatheterPresenter presenter;
    ISafeOperationSignPresenter presenter_sign;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_catheter_sign);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loginDialog = new LoginDialog(aty);
        loadingDialog = new LoadingDialog(aty);
        presenter = new CatheterPresenter(this,new CatheterModel());
        presenter_sign = new SafeOperationSignPresenter(this,new SafeOperationSignModel());
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.CATHETERBEAN.getObject().getCatheter().getPatientName());
        if (AppConfig.CATHETERBEAN.getObject().getCatheter().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.CATHETERBEAN.getObject().getCatheter().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.CATHETERBEAN.getObject().getCatheter().getInHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }

    private void setContent()
    {
        if(AppConfig.CATHETERBEAN.getObject().getCatheter().getOperatorSign()!=null
                &&AppConfig.CATHETERBEAN.getObject().getCatheter().getOperatorSign().length()>0){
            imgSignVisit.setVisibility(View.VISIBLE);
            btnSignVisit.setVisibility(View.GONE);
            tvSignVisitTime.setVisibility(View.VISIBLE);
            tvSignVisitName.setVisibility(View.VISIBLE);
            Glide.with(aty)
                    .load(AppConfig.CATHETERBEAN.getObject().getCatheter().getOperatorSign())
                    .placeholder(R.color.white)
                    .error(R.color.white)
                    .into(imgSignVisit);
            tvSignVisitTime.setText("签名时间:"+AppConfig.CATHETERBEAN.getObject().getCatheter().getOperationDate());
            tvSignVisitName.setText(AppConfig.CATHETERBEAN.getObject().getCatheter().getOperator());
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.btn_sign_visit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.btn_sign_visit:
                showTypeDialog();
                break;
        }
    }

    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        actionSheetDialog.addSheetItem("HIS签名", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        loginDialog.builder();
                        loginDialog.show();
                        loginDialog.setCallBack(new LoginDialog.ICallBack() {
                            @Override
                            public void callback(Bundle bundle) {
                                Map<String, String> params = new HashMap<>();
                                params.put("jobNum", bundle.getString("person_num"));
                                params.put("loginPwd", bundle.getString("person_psw"));
                                presenter_sign.getSignInfo(params);
                            }
                        });
                    }
                });
        actionSheetDialog.addSheetItem("虹膜签名(暂不可用)", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {

                    }
                });
        actionSheetDialog.show();
    }

    @Override
    public void insertCatheterDate(SubmitBean submitBean) {
        if(submitBean.getCode() == 200)
        {
            ViewInject.toast("保存成功");
            finish();
        }
        else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateCatheterDate(SubmitBean submitBean) {
        if(submitBean.getCode() == 200)
        {
            ViewInject.toast("保存成功");
            finish();
        }
        else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void getSignInfo(SignBean signBean) {
        loginDialog.dismiss();
        if(signBean.getCode() == 200)
        {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault());
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String str = formatter.format(curDate);
            imgSignVisit.setVisibility(View.VISIBLE);
            Glide.with(this)
                    .load(signBean.getObject().getSignPicture())
                    .placeholder(R.color.white)
                    .error(R.color.white)
                    .into(imgSignVisit);
            btnSignVisit.setVisibility(View.GONE);
            tvSignVisitTime.setVisibility(View.VISIBLE);
            tvSignVisitTime.setText("签名时间:" + str);
            tvSignVisitName.setVisibility(View.VISIBLE);
            tvSignVisitName.setText(signBean.getObject().getDoctorName());

            AppConfig.CATHETERBEAN.getObject().getCatheter().setOperator(signBean.getObject().getDoctorName());
            AppConfig.CATHETERBEAN.getObject().getCatheter().setOperatorSign(signBean.getObject().getSignPicture());
            AppConfig.CATHETERBEAN.getObject().getCatheter().setOperationDate(str);

            if(AppConfig.CATHETERBEAN.getObject().getCatheter().getCatheterId() == null) {
                Map<String, String> params = new HashMap<>();
                params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID)+"");
                params.put("saveData", JSON.toJSONString(AppConfig.CATHETERBEAN));
                presenter.insertCatheterDate(params);
            }
            else {
                Map<String, String> params = new HashMap<>();
                params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
                params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.CATHETERBEAN));
                presenter.updateCatheterDate(params);
            }
        }
        else {
            ViewInject.toast(signBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
